package com.macro.mall.tiny.component.wx.entity.bo;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyV3Result;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import static com.github.binarywang.demo.wx.pay.config.WxPayConfiguration.NOTIFY_URL;

/**
 * @author Tyrone
 * @date 2024/10/23 21:32
 */
@Data
@Slf4j
public class WxPayBO {

    // 以下字段为基础字段请求和响应都会有


    /**
     * 支付用户openId
     */
    private String openId;



    // 以下字段为请求字段

    /**
     * 描述
     */
    private DescEnum desc;

    /**
     * 内部订单号
     */
    private String innerOrderNo;


    /**
     * 总价金额 单位分
     */
    private Integer total;

    // 以下字段为响应字段

    /**
     * 交易状态
     */
    private String tradeState;

    /**
     * 支付成功时间
     */
    private Date successTime;

    public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");


    public static WxPayBO extract(WxPayNotifyV3Result wxPayNotifyV3Result) {
        WxPayBO payBO = new WxPayBO();
        try {
            String openid = wxPayNotifyV3Result.getResult().getPayer().getOpenid();
            String tradeState = wxPayNotifyV3Result.getResult().getTradeState();
            //字段格式为2018-06-08T10:34:56+08:00
            String successTime = wxPayNotifyV3Result.getResult().getSuccessTime();
            LocalDateTime localDateTime = LocalDateTime.parse(successTime, formatter);
            Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
            payBO.setInnerOrderNo(wxPayNotifyV3Result.getResult().getOutTradeNo());
            payBO.setOpenId(openid);
            payBO.setTradeState(tradeState);
            payBO.setSuccessTime(date);
        }catch (Exception e){
            log.error("com.macro.mall.portal.component.wx.entity.bo.WxPayBO#extract error: {}", e.getMessage());
        }

        return payBO;
    }


    public WxPayUnifiedOrderV3Request buildUnifiedOrderRequest() {
        WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();

        Assert.notNull(this.desc, "描述");
        Assert.notNull(this.innerOrderNo, "内部订单号不能为空");
        Assert.notNull(this.total, "total不能为空");
        Assert.notNull(this.openId, "openId不能为空");

        request.setDescription(this.desc.getDesc());
        request.setOutTradeNo(this.innerOrderNo);
        request.setAmount(new WxPayUnifiedOrderV3Request.Amount().setTotal(this.total));
        request.setPayer(new WxPayUnifiedOrderV3Request.Payer().setOpenid(this.openId));
        request.setNotifyUrl(NOTIFY_URL);

        return request;
    }


    public enum DescEnum {

        ONE_YEAR(1, "师傅维修助手-VIP(1年)"),

        THREE_YEAR(2, "师傅维修助手-VIP(3年)"),

        FOREVER(3, "师傅维修助手-VIP(永久)"),

        TWELVE_MONTH(4, "师傅维修助手-VIP(12个月)");

        private Integer code;
        private String desc;

        DescEnum(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public String getDesc() {
            return desc;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public static DescEnum getDescEnum(Integer code) {
            for (DescEnum descEnum : DescEnum.values()) {
                if (descEnum.getCode().equals(code)) {
                    return descEnum;
                }
            }
            return null;
        }
    }
}
